package com.haoshuang.zhouzhoubang.ui.page.home.settings.skill.passed

import android.content.Context
import android.os.Bundle
import android.util.Log
import com.gyf.immersionbar.ktx.immersionBar
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityAddSkillListBinding
import com.haoshuang.zhouzhoubang.databinding.ActivityPassedSkillListBinding
import com.haoshuang.zhouzhoubang.model.bean.SkillAddBean
import com.haoshuang.zhouzhoubang.ui.page.home.settings.skill.add.AddSkillLeftAdapter
import com.haoshuang.zhouzhoubang.ui.page.home.settings.skill.add.AddSkillListViewModel
import com.haoshuang.zhouzhoubang.ui.page.home.settings.skill.add.AddSkillRightAdapter
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.ktx.toJson
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.invisible
import com.mufeng.libs.utils.view.removeAllAnimation
import com.mufeng.libs.utils.view.textColor
import com.mufeng.libs.utils.view.vertical
import com.mufeng.libs.utils.view.visible

/**
 *  门店已经添加通过的技能列表
 */
class PassedSkillListActivity : BaseVMActivity<PassedSkillListViewModel, ActivityPassedSkillListBinding>() {

    companion object {
        fun launch(context: Context){
            context.startPage<PassedSkillListActivity>()
        }
    }

    private var type = 2

    private val leftData = arrayListOf<SkillAddBean>()
    private val rightData = arrayListOf<SkillAddBean.Small>()

    private val leftAdapter = PassedSkillLeftAdapter(leftData)
    private val rightAdapter = PassedSkillRightAdapter(rightData)

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "已通过技能列表"
        binding.titleBar.leftImageView().click { finish() }

        binding.rvList.vertical().adapter = leftAdapter
        binding.rvList.removeAllAnimation()
        binding.rvList2.vertical().adapter = rightAdapter
        binding.rvList2.removeAllAnimation()

        leftAdapter.setOnItemClick { view, position, item ->
            leftData.forEach {
                it.is_checked = item?.id == it.id
            }
            leftAdapter.notifyDataSetChanged()
            rightData.clear()
            rightData.addAll(item?.small ?: listOf())
            rightAdapter.notifyDataSetChanged()
        }
    }

    override fun initData() {
        vm.getAddSkillList(type)
    }

    override fun startObserve() {

        vm.getAddSkillListData.observeState(this){
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
                leftData.clear()
                leftAdapter.notifyDataSetChanged()
                rightData.clear()
                rightAdapter.notifyDataSetChanged()
            }
            onSuccess {
                dismissLoading()

                val list = mutableListOf<SkillAddBean>()
                // 过滤数据
                // 第一层过滤: 把三级为空的二级分类给去掉
                it?.data?.forEach {first ->
                    val secondList = first.small?.filter { !it.little_cate.isNullOrEmpty() }?.toMutableList()
                    if (secondList?.isNotEmpty() == true){
                        first.small = secondList
                        list.add(first)
                    }
                }
                if (list.isNotEmpty()){
                    list[0].is_checked = true
                    leftData.clear()
                    leftData.addAll(list.filter { it.small?.isNotEmpty() == true })
                    leftAdapter.notifyDataSetChanged()
                    rightData.clear()
                    rightData.addAll(list[0].small ?: listOf())
                    rightAdapter.notifyDataSetChanged()
                }else{
                    leftData.clear()
                    leftAdapter.notifyDataSetChanged()
                    rightData.clear()
                    rightAdapter.notifyDataSetChanged()
                }

            }
        }

    }

    override fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(false)
            transparentStatusBar()
            titleBarMarginTop(binding.titleBar)
        }
    }

}